package com.whlilong.jxc.dialog;

import java.util.ArrayList;
import java.util.List;


import org.eclipse.swt.SWT;

import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.whlilong.jxc.db.DbManager;
import com.whlilong.jxc.model.Keshang;
import com.whlilong.jxc.model.Khinfo;
import com.whlilong.jxc.model.Kstype;
import com.whlilong.jxc.sys.ExtentionInfo;

public class ClientInfoSearchByConditon extends AbstractDialog {

	public ClientInfoSearchByConditon(Shell parentShell) {
		super(parentShell);
		// TODO Auto-generated constructor stub
	}

	private List<Khinfo> list = new ArrayList<Khinfo>();
	
	protected ExtentionInfo extentionInfo = null;

	private Group group;

	private Label khbhLabel;

	private Text khbh;

	private Label khnameLabel;

	private Text khname;

	private Label pymLabel;

	private Text pymText;

	private Label yzbmLabel;

	private Text yzbm;

	private Label khqyLabel;

	private Combo khqy;

	private Label khtpLabel;

	private Combo khtp;

	private Label telLabel;

	private Text tel;

	private Label addrLabel;

	private Text addr;

	private Label lxrLabel;

	private Text lxr;

	private Label faxLabel;

	private Text fax;

	private Label mailLabel;

	private Text mail;

	private Label yhLabel;

	private Text yh;

	private Button khbhcheck;

	private Button khnamecheck;

	private Button pymcheck;

	private Button yzbmcheck;

	private Button khqycheck;

	private Button khtpcheck;

	private Button telcheck;

	private Button addrcheck;

	private Button lxrcheck;

	private Button faxcheck;

	private Button mailcheck;

	private Button yhcheck;

	private Button selectAll;

	private String khid;

	private String khName;

	private String khareaname;

	private String khtpname;

	private String khtel;

	private String khaddress;

	private String people;

	private String faxNum;

	private String yzbmNum;

	private String yhNum;

	private String Email;

	private String pym;
	private FormData fd_1;
	
	@SuppressWarnings("unchecked")
	public Control createGroupText(Composite parent) {
		FormLayout formLayout = (FormLayout) parent.getLayout();
		formLayout.marginTop = 20;
		formLayout.marginLeft = 10;
		// TODO Auto-generated method stub
		group = new Group(parent, SWT.NONE);
		FormData fd = new FormData();
		fd.left = new FormAttachment(10, -20);
		fd.right = new FormAttachment(90, 20);
		fd.top = new FormAttachment(0, -10);
		group.setLayoutData(fd);
		group.setText("条件查询");

		FormLayout layout = new FormLayout();
		layout.marginTop = 5;
		layout.marginLeft = 5;
		layout.marginRight = 5;
		group.setLayout(layout);

		Composite composite = new Composite(group, SWT.NONE);
		composite.setLayoutData(new FormData());
		GridLayout gl = new GridLayout(6, false);
		composite.setLayout(gl);

		// 第一行
		GridData gd = new GridData();
		gd.widthHint = 75;
		gd.horizontalIndent = 5;
		gd.verticalIndent = 3;
		// 第二行
		khbhLabel = new Label(composite, SWT.RIGHT);
		khbhLabel.setText("客户编号：");
		khbhLabel.setLayoutData(gd);
		khbh = new Text(composite, SWT.BORDER);
		GridData gd_khbh = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_khbh.widthHint = 90;
		khbh.setLayoutData(gd_khbh);
		khbh.setEnabled(false);
		khbhcheck = new Button(composite, SWT.RIGHT | SWT.CHECK);
		GridData gd_khbhcheck = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_khbhcheck.widthHint = 35;
		khbhcheck.setLayoutData(gd_khbhcheck);

		khnameLabel = new Label(composite, SWT.RIGHT);
		khnameLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		khnameLabel.setText("客户名称：");
		khname = new Text(composite, SWT.BORDER);
		GridData gd_khname = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_khname.widthHint = 90;
		khname.setLayoutData(gd_khname);
		khname.setEnabled(false);
		khnamecheck = new Button(composite, SWT.RIGHT | SWT.CHECK);

		pymLabel = new Label(composite, SWT.RIGHT);
		pymLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		pymLabel.setText("拼音码：");
		pymText = new Text(composite, SWT.BORDER);
		GridData gd_pymText = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_pymText.widthHint = 90;
		pymText.setLayoutData(gd_pymText);
		pymText.setEnabled(false);
		pymcheck = new Button(composite, SWT.RIGHT | SWT.CHECK);
		GridData gd_pymcheck = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_pymcheck.widthHint = 35;
		pymcheck.setLayoutData(gd_pymcheck);

		yzbmLabel = new Label(composite, SWT.RIGHT);
		yzbmLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		yzbmLabel.setText("邮政编码：");
		yzbm = new Text(composite, SWT.BORDER);
		GridData gd_yzbm = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_yzbm.widthHint = 90;
		yzbm.setLayoutData(gd_yzbm);
		yzbm.setEnabled(false);
		yzbmcheck = new Button(composite, SWT.RIGHT | SWT.CHECK);

		// 第三行

		khqyLabel = new Label(composite, SWT.RIGHT);
		khqyLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		khqyLabel.setText("客户区域：");
		khqy = new Combo(composite, SWT.READ_ONLY);
		GridData gd_khqy = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_khqy.widthHint = 74;
		khqy.setLayoutData(gd_khqy);
		khqy.setEnabled(false);
		khqy.add("不定");
		List kslist = extentionInfo.getInstance().getKeshanginfo();
		for (int k = 0; k < kslist.size(); k++) {
			Keshang ksarea = (Keshang) kslist.get(k);
			khqy.add(ksarea.getKeshangarea());
		}
		khqy.select(0);
		khqycheck = new Button(composite, SWT.RIGHT | SWT.CHECK);

		khtpLabel = new Label(composite, SWT.RIGHT);
		khtpLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		khtpLabel.setText("客户类别：");
		khtp = new Combo(composite, SWT.READ_ONLY);
		GridData gd_khtp = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_khtp.widthHint = 74;
		khtp.setLayoutData(gd_khtp);
		khtp.setEnabled(false);
		khtp.add("不定");
		List kstplist = extentionInfo.getInstance().getKstypeinfo();
		for (int k = 0; k < kstplist.size(); k++) {
			Kstype kstype = (Kstype) kstplist.get(k);
			khtp.add(kstype.getKstype());
		}
		khtp.select(0);
		khtpcheck = new Button(composite, SWT.RIGHT | SWT.CHECK);

		telLabel = new Label(composite, SWT.RIGHT);
		telLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		telLabel.setText("客户电话：");
		tel = new Text(composite, SWT.BORDER);
		GridData gd_tel = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_tel.widthHint = 90;
		tel.setLayoutData(gd_tel);
		tel.setEnabled(false);
		telcheck = new Button(composite, SWT.RIGHT | SWT.CHECK);

		addrLabel = new Label(composite, SWT.RIGHT);
		addrLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		addrLabel.setText("客户地址：");
		addr = new Text(composite, SWT.BORDER);
		GridData gd_addr = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_addr.widthHint = 90;
		addr.setLayoutData(gd_addr);
		addr.setEnabled(false);
		addrcheck = new Button(composite, SWT.RIGHT | SWT.CHECK);

		// 第四行

		lxrLabel = new Label(composite, SWT.RIGHT);
		lxrLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lxrLabel.setText("联系人：");
		lxr = new Text(composite, SWT.BORDER);
		GridData gd_lxr = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_lxr.widthHint = 90;
		lxr.setLayoutData(gd_lxr);
		lxr.setEnabled(false);
		lxrcheck = new Button(composite, SWT.RIGHT | SWT.CHECK);

		faxLabel = new Label(composite, SWT.RIGHT);
		faxLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		faxLabel.setText("传真：");
		fax = new Text(composite, SWT.BORDER);
		GridData gd_fax = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_fax.widthHint = 90;
		fax.setLayoutData(gd_fax);
		fax.setEnabled(false);
		faxcheck = new Button(composite, SWT.RIGHT | SWT.CHECK);

		mailLabel = new Label(composite, SWT.RIGHT);
		mailLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		mailLabel.setText("E-Mail：");
		mail = new Text(composite, SWT.BORDER);
		GridData gd_mail = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_mail.widthHint = 90;
		mail.setLayoutData(gd_mail);
		mail.setEnabled(false);
		mailcheck = new Button(composite, SWT.RIGHT | SWT.CHECK);

		yhLabel = new Label(composite, SWT.RIGHT);
		yhLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		yhLabel.setText("银行账号：");
		yh = new Text(composite, SWT.BORDER);
		GridData gd_yh = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_yh.widthHint = 90;
		yh.setLayoutData(gd_yh);
		yh.setEnabled(false);
		yhcheck = new Button(composite, SWT.RIGHT | SWT.CHECK);

		selectAll = new Button(parent, SWT.CHECK | SWT.RIGHT);
		selectAll.setText("全选(&A)");
		fd_1 = new FormData();
		fd_1.top = new FormAttachment(group, 10);
		fd_1.bottom = new FormAttachment(100);
		fd_1.left = new FormAttachment(85, 0);
		selectAll.setLayoutData(fd_1);

		addListener();
		
		return parent;

	}

	private void addListener() {
		// TODO Auto-generated method stub
		khbhcheck.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				if (khbhcheck.getSelection())
					khbh.setEnabled(true);
				else
				{
					khbh.setText("");
					khbh.setEnabled(false);
				}
				update();
			}
		});
		khnamecheck.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				if (khnamecheck.getSelection())
					khname.setEnabled(true);
				else{
					khname.setText("");
					khname.setEnabled(false);
				}
				update();
			}
		});

		pymcheck.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				if (pymcheck.getSelection())
					pymText.setEnabled(true);
				else{
					pymText.setText("");
					pymText.setEnabled(false);
				}
				update();
			}
		});
		yzbmcheck.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				if (yzbmcheck.getSelection())
					yzbm.setEnabled(true);
				else{
					yzbm.setText("");
					yzbm.setEnabled(false);
				}
				update();
			}
		});
		khqycheck.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				if (khqycheck.getSelection())
					khqy.setEnabled(true);
				else{
					khqy.select(0);
					khqy.setEnabled(false);
				}
				update();
			}
		});
		khtpcheck.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				if (khtpcheck.getSelection())
					khtp.setEnabled(true);
				else{
					khtp.select(0);
					khtp.setEnabled(false);
				}
				update();
			}
		});
		telcheck.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				if (telcheck.getSelection())
					tel.setEnabled(true);
				else{
					tel.setText("");
					tel.setEnabled(false);
				}
				update();
			}
		});
		addrcheck.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				if (addrcheck.getSelection())
					addr.setEnabled(true);
				else{
					addr.setText("");
					addr.setEnabled(false);
				}
				update();
			}
		});
		lxrcheck.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				if (lxrcheck.getSelection())
					lxr.setEnabled(true);
				else{
					lxr.setText("");
					lxr.setEnabled(false);
				}
				update();
			}
		});
		faxcheck.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				if (faxcheck.getSelection())
					fax.setEnabled(true);
				else{
					fax.setText("");
					fax.setEnabled(false);
				}
				update();
			}
		});
		mailcheck.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				if (mailcheck.getSelection())
					mail.setEnabled(true);
				else{
					mail.setText("");
					mail.setEnabled(false);
				}
				update();
			}
		});
		yhcheck.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				if (yhcheck.getSelection())
					yh.setEnabled(true);
				else{
					yh.setText("");
					yh.setEnabled(false);
				}
				update();
			}
		});
		selectAll.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				if (selectAll.getSelection()) {
					khbhcheck.setSelection(true);
					khnamecheck.setSelection(true);
					pymcheck.setSelection(true);
					yzbmcheck.setSelection(true);
					khqycheck.setSelection(true);
					khtpcheck.setSelection(true);
					telcheck.setSelection(true);
					addrcheck.setSelection(true);
					lxrcheck.setSelection(true);
					faxcheck.setSelection(true);
					mailcheck.setSelection(true);
					yhcheck.setSelection(true);
				} else {
					khbhcheck.setSelection(false);
					khnamecheck.setSelection(false);
					pymcheck.setSelection(false);
					yzbmcheck.setSelection(false);
					khqycheck.setSelection(false);
					khtpcheck.setSelection(false);
					telcheck.setSelection(false);
					addrcheck.setSelection(false);
					lxrcheck.setSelection(false);
					faxcheck.setSelection(false);
					mailcheck.setSelection(false);
					yhcheck.setSelection(false);
				}
				initail();
			}
		});
	}

	private void update() {
		// TODO Auto-generated method stub
		if (khbhcheck.getSelection() && khnamecheck.getSelection()
				&& pymcheck.getSelection() && yzbmcheck.getSelection()
				&& khqycheck.getSelection() && khtpcheck.getSelection()
				&& telcheck.getSelection() && addrcheck.getSelection()
				&& lxrcheck.getSelection() && faxcheck.getSelection()
				&& mailcheck.getSelection() && yhcheck.getSelection()) {
			selectAll.setSelection(true);
		}
		else
			selectAll.setSelection(false);
	}

	private void initail() {
		// TODO Auto-generated method stub
		if (khbhcheck.getSelection())
			khbh.setEnabled(true);
		else{
			khbh.setText("");
			khbh.setEnabled(false);
		}
		if (khnamecheck.getSelection())
			khname.setEnabled(true);
		else{
			khname.setText("");
			khname.setEnabled(false);
		}
		if (pymcheck.getSelection())
			pymText.setEnabled(true);
		else{
			pymText.setText("");
			pymText.setEnabled(false);
		}
		if (yzbmcheck.getSelection())
			yzbm.setEnabled(true);
		else{
			yzbm.setText("");
			yzbm.setEnabled(false);
		}
		if (khqycheck.getSelection())
			khqy.setEnabled(true);
		else{
			khqy.select(0);
			khqy.setEnabled(false);
		}
		if (khtpcheck.getSelection())
			khtp.setEnabled(true);
		else{
			khtp.select(0);
			khtp.setEnabled(false);
		}
		if (telcheck.getSelection())
			tel.setEnabled(true);
		else{
			tel.setText("");
			tel.setEnabled(false);
		}
		if (addrcheck.getSelection())
			addr.setEnabled(true);
		else{
			addr.setText("");
			addr.setEnabled(false);
		}
		if (lxrcheck.getSelection())
			lxr.setEnabled(true);
		else{
			lxr.setText("");
			lxr.setEnabled(false);
		}
		if (faxcheck.getSelection())
			fax.setEnabled(true);
		else{
			fax.setText("");
			fax.setEnabled(false);
		}
		if (mailcheck.getSelection())
			mail.setEnabled(true);
		else{
			mail.setText("");
			mail.setEnabled(false);
		}
		if (yhcheck.getSelection())
			yh.setEnabled(true);
		else{
			yh.setText("");
			yh.setEnabled(false);
		}
	}
	
	@SuppressWarnings("unchecked")
	public void exeSearch() {
		khid = khbh.getText();
		khName = khname.getText();
		khareaname = khqy.getText();
		khtpname = khtp.getText();
		khtel = tel.getText();
		khaddress = addr.getText();
		people = lxr.getText();
		faxNum = fax.getText();
		yzbmNum = yzbm.getText();
		Email = mail.getText();
		yhNum = yh.getText();
		pym = pymText.getText();
		StringBuffer hql = new StringBuffer("from Khinfo where 1=1");
		List paramlist = new ArrayList();
		List valuelist = new ArrayList();
		int count = 0;
		if(!khid.equals("")){
			hql.append(" and khid=?");
			paramlist.add(count);
			valuelist.add(khid);
			count++;
		}
		if(!khName.equals("")){
			hql.append(" and khname=?");
			paramlist.add(count);
			valuelist.add(khName);
			count++;
		}
		if(khqycheck.getSelection()){
			hql.append(" and khareaname=?");
			paramlist.add(count);
			valuelist.add(khareaname);
			count++;
		}
		if(khtpcheck.getSelection()){
			hql.append(" and khtpname=?");
			paramlist.add(count);
			valuelist.add(khtpname);
			count++;
		}
		if(!khtel.equals("")){
			hql.append(" and khtel=?");
			paramlist.add(count);
			valuelist.add(khtel);
			count++;
		}
		if(!khaddress.equals("")){
			hql.append(" and khaddress=?");
			paramlist.add(count);
			valuelist.add(khaddress);
			count++;
		}
		if(!people.equals("")){
			hql.append(" and people=?");
			paramlist.add(count);
			valuelist.add(people);
			count++;
		}
		if(!faxNum.equals("")){
			hql.append(" and fax=?");
			paramlist.add(count);
			valuelist.add(faxNum);
			count++;
		}
		if(!yzbmNum.equals("")){
			hql.append(" and yzbm=?");
			paramlist.add(count);
			valuelist.add(yzbmNum);
			count++;
		}
		if(!yhNum.equals("")){
			hql.append("and yh=?");
			paramlist.add(count);
			valuelist.add(yhNum);
			count++;
		}
		if(!pym.equals("")){
			hql.append(" and pym =?");
			paramlist.add(count);
			valuelist.add(pym.toUpperCase());
			count++;
		}
		if(!Email.equals("")){
			hql.append(" and mail=?");
			paramlist.add(count);
			valuelist.add(Email);
			count++;
		}
		Integer[] param = new Integer[paramlist.size()];
		Object[] values = new Object[valuelist.size()];
		values = valuelist.toArray(values);
		param = (Integer[]) paramlist.toArray(param);
		list = DbManager.getInstance().search(hql.toString(),param, values);
	}

	public List<Khinfo> getList() {
		return list;
	}

	public void setList(List<Khinfo> list) {
		this.list = list;
	}

}
